function addStyle(css) {
    if (typeof GM_addStyle !== 'undefined') return GM_addStyle(css);
    else if (heads = document.getElementsByTagName('head')) {
        var style = document.createElement('style');
        try { style.innerHTML = css; }
        catch (x) { style.innerText = css; }
        style.type = 'text/css';
        heads[0].appendChild(style);
    }
}

addStyle(
  '.fbfPopup { background:#f6f6f6; border:3px double #666666; }' +
  '.fbfPopupContainer { display:none; top:0; right:0; bottom:0; left:0; }' +
  '#IMGPdiv { display:none; position:fixed !important; top:2px !important; padding:2px 4px; min-width:130px; z-index:99999 !important;}' +
  '.IMGPdivLeft { left:2px !important; right:auto !important; }' +
  '.IMGPdivRight { right:2px !important; left:auto !important; }' +
  '#IMGPdiv img { max-height: ' + (window.innerHeight - 35) + 'px; }' +
  '#IMGPheader, #IMGPloading { text-align:center; color:#3366cc; font-variant:small-caps; font-weight:bold !important; }' +
  '#IMGPclose { text-align:right; color:#ffaaaa; cursor:pointer; font-weight:bold; height:1px; }' +
  '#IMGPclose:hover { color:#aa6666; }' +
  '#IMGPimg { text-align:center; }' +
  '#IMGPimg img { color:#999999; }' +
  '#FBFBigAlbumContainer { padding:0 0 40px; }' +
  '#FBFBigAlbum { padding:3px 3px 20px; margin:35px 15px 0px; text-align:center; }' +
  '#FBFBigAlbum img { padding:1px; }' +
  '.FBFBigAlbumClose { float:right; color:red; cursor:pointer; font-weight:bold; background:#fff9f9; padding:0 10px; border:1px solid #f6f6f0; }' +
  '#FBFConfigContainer { z-index:1001; }' +
  '#FBFConfig { width:700px; padding:10px; margin:20px auto 0; }' +
  '#FBFConfig label, #FBFConfig .fbfLabel { color:#666666; font-weight:normal; } ' +
  '#FBFConfig .fbfHeader { font-weight:bold; }' +
  '#FBFConfigShadow, #fbfShadow { display:none; position:fixed; top:0; left:0; right:0; bottom:0; background:black; opacity:0.6; }' +
  '#fbfKeyboardShortcutsList { display:none; }' +
  '#fbfUpdatePopup { max-width:450px; margin:100px auto; padding:10px; }' +
  '.fbfImportant { font-weight:bold; }' +
  '.fbfNote { color:#777777; }' +
  '.fbfRight { text-align:right; }'
);

//ADD DIV FOR SHOWING BIG PROFILE PICS
//
var div = document.createElement('div');
div.id = 'IMGPdiv';
div.className = 'fbfPopup IMGPdivLeft';
div.innerHTML = '<div id="IMGPclose" title="Close">x</div><div id="IMGPheader">Image Preview</div><div id="waitingImg" style="vertical-align: middle; text-align: center;"><img src="'+ chrome.extension.getURL("images/waiting.gif") +'"/></div><div id="IMGPimg"></div>';
try {
    document.body.insertBefore(div, document.body.lastChild.nextSibling);
    document.getElementById('IMGPclose').addEventListener('click', function() { document.getElementById('IMGPdiv').style.display = 'none'; }, false);
} catch (x) {
    var IMGPdivAdder = setInterval(function() {
        //log('waiting to add IMGPdiv'); // DEBUG ONLY
        try {
            document.body.insertBefore(div, document.body.lastChild.nextSibling);
            document.getElementById('IMGPclose').addEventListener('click', function() { document.getElementById('IMGPdiv').style.display = 'none'; }, false);
            if ($('IMGPdiv')) { clearInterval(IMGPdivAdder); }
        } catch (x) { }
    }, 100);
}

//LISTEN FOR IMAGE MOUSEOVERS/MOUSEOUTS TO SHOW/HIDE POPUPS
//
picRegex = /:\/\/imgres\?imgurl=(imgUrl).*/;
function showPopupPic(e) {
    try {
        var t = e.target;
        if (t.tagName == 'IMG') {
            var oldSrc = new Image();
            oldSrc.src = t.src;
            var newSrc;
            var re = new RegExp('imgurl=(.*?)&');
            var rejpg = new RegExp('http:(.*?).jpg');
            var regif = new RegExp('http:(.*?).gif');
            var repng = new RegExp('http:(.*?).png');
            if (t.parentNode.tagName == 'A' && re.test(t.parentNode.href)) {
                var matches = re.exec(t.parentNode.href);
                newSrc = new Image();
                newSrc.src = matches[1].replace(/%2520/g,'%20');
                $('#waitingImg').css('display', '');
            	$('#IMGPimg').html("<img src='" + newSrc.src + "' onload=\"document.getElementById('waitingImg').style.display = 'none'\" onerror=\"document.getElementById('waitingImg').style.display = 'none';document.getElementById('IMGPimg').innerHTML='<span>Image not found</span>';\" alt='LoadingPic' />");
                $('#IMGPdiv').css("display", "block");
                $('#IMGPdiv').removeClass('fbfPopup IMGPdivLeft');
                $('#IMGPdiv').removeClass('fbfPopup IMGPdivRight');
            	$('#IMGPdiv').addClass('fbfPopup IMGPdiv' + (e.pageX > document.body.clientWidth / 2 ? 'Left' : 'Right'));
            }
            else if (t.parentNode.tagName == 'A' && (rejpg.test(t.parentNode.href) || regif.test(t.parentNode.href) || repng.test(t.parentNode.href) ))
            {
                newSrc = new Image();
                newSrc.src = t.parentNode.href.replace(/%2520/g,'%20');
                $('#waitingImg').css('display', '');
            	$('#IMGPimg').html("<img src='" + newSrc.src + "' onload=\"document.getElementById('waitingImg').style.display = 'none'\" onerror=\"document.getElementById('waitingImg').style.display = 'none';document.getElementById('IMGPimg').innerHTML='<span>Image not found</span>';\" alt='LoadingPic' />");
                $('#IMGPdiv').css("display", "block");
                $('#IMGPdiv').removeClass('fbfPopup IMGPdivLeft');
                $('#IMGPdiv').removeClass('fbfPopup IMGPdivRight');
            	$('#IMGPdiv').addClass('fbfPopup IMGPdiv' + (e.pageX > document.body.clientWidth / 2 ? 'Left' : 'Right'));
            }
            else {
                if (t.clientHeight != t.naturalHeight || t.clientWidth != t.naturalWidth) {
                	$('#waitingImg').css('display', '');
                    $('#IMGPimg').html("<img src='" + oldSrc.src + "' onload=\"document.getElementById('waitingImg').style.display = 'none'\" onerror=\"document.getElementById('waitingImg').style.display = 'none';document.getElementById('IMGPimg').innerHTML='<span>Image not found</span>';\" alt='LoadingPic' />");
                    $('#IMGPdiv').css('display', 'block');
                    $('#IMGPdiv').removeClass('fbfPopup IMGPdivLeft');
                    $('#IMGPdiv').removeClass('fbfPopup IMGPdivRight');
                    $('#IMGPdiv').addClass('fbfPopup IMGPdiv' + (e.pageX > document.body.clientWidth / 2 ? 'Left' : 'Right'));
                }
            }
        }
    } catch (x) { console.log('PopupPic Error! => ' + x.message + (x.description != null ? '\n' + x.description : '')); }
}

var lastEventMouseOver;

function checkAndViewImage() {
    if (null === lastEventMouseOver)
        return;
    
    var t = lastEventMouseOver.target;
    if (t.tagName != 'IMG')
    	return;

    myport = chrome.extension.connect({ name: "ShowImage" });
    myport.postMessage({ message: "askModal" });
    myport.onMessage.addListener(function(event) {
        if (event.message == "returnModal") {
            if ((event.use_shift == lastEventMouseOver.shiftKey) &&
			     (event.use_ctrl == lastEventMouseOver.ctrlKey) &&
			     (event.use_alt == lastEventMouseOver.altKey))
                showPopupPic(lastEventMouseOver);
        }
    });
}

window.addEventListener('mouseover', function(e) {
    lastEventMouseOver = e;
    checkAndViewImage();
}, false);

document.addEventListener('mouseout', function(e) {
    lastEventMouseOver = null;
    $('#IMGPdiv').css("display", "none");
}, false);